package com.financing.platform.web;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.financing.platform.dto.OrderDTO;
import com.financing.platform.service.IFaUserService;
import com.financing.platform.service.IOrderService;
import com.financing.platform.service.IProjectService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;
import com.financing.platform.vo.OrderVO;

@Controller
public class OrderAction  extends BaseAction{
	
	@Autowired
	IOrderService orderServiceImpl;
	
	@Autowired
	IProjectService projectServiceImpl;
	
	@Autowired
	IFaUserService faUserServiceImpl;
	
	@RequestMapping("/queryOrderListPer.shtml")
	public ModelAndView queryOrderListPer(HttpServletRequest request,
			HttpServletResponse response ) {
		
		request.getSession().setAttribute(Dict.requestID, "queryOrderListPer.shtml");
		
		return new ModelAndView("orderListPer");
	}
	
	@RequestMapping("/queryOrderList.shtml")
	public ModelAndView queryOrderList(HttpServletRequest request,
			HttpServletResponse response,OrderVO orderVo) {
		
		List<OrderVO> listOrder = orderServiceImpl.queryOrderList(orderVo);
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		
		resultMap.put("orderList", listOrder);
		
		return new ModelAndView("orderList",resultMap);
	}
	
	@RequestMapping("/queryOrderDetail.shtml")
	public ModelAndView queryOrderDetail(HttpServletRequest request,
			HttpServletResponse response,OrderVO orderVo ) {
		
		List<OrderVO> listOrder = orderServiceImpl.queryOrderList(orderVo);
		
		if(listOrder.size()>0){
			orderVo = listOrder.get(0);
		} 
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("orderDetail", orderVo);
		
		return new ModelAndView("orderDetail",resultMap);
	}
	
	@RequestMapping("/updateOrderStatus.shtml")
	public ModelAndView updateOrderStatus(HttpServletRequest request,
			HttpServletResponse response,OrderDTO orderDTO ) {
		
		DTOdateSet.setUpdateInfo(orderDTO, request.getSession());

		StringBuffer stringBuffer = new StringBuffer()
										.append("order ")
										.append(orderDTO.getId())
										.append("status update to ")
										.append(orderDTO.getoStatus());
		int totalUpdate = 0;
		try {
			logsServiceImpl.addLog("updateOrderStatus order id :"+orderDTO.getId(), stringBuffer.toString(), orderDTO.getUpdateBy());
			totalUpdate = orderServiceImpl.updateOrderStatus(orderDTO);
		} catch (Exception e) {
			log.error("update order status error ",e);
		}
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("result", totalUpdate);
		
		return new ModelAndView("orderUpdateStatusResult",resultMap);
	}
	
}
